<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>实体列表</title>
        <meta content='width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no' name='viewport'>
        <!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
        <!--[if lt IE 9]>
        <script type="text/javascript"  src="http://www.ijquery.cn/js/html5shiv.js"></script>
        <![endif]-->
        <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
        <script>window.jQuery || document.write('<script src="js/jquery-1.12.4.min.js">\x3C/script>')</script>
        <!--也可以用yepnopejs 加载器;直接是window对象-->
        <!--Font Awesome  一套绝佳的图标字体库和CSS框架 无需依赖JavaScript 无限缩放  完全免费 jquery-confirm里面用到 官网http://fontawesome.dashgame.com/-->
        <link href="css/font-awesome/css/font-awesome.min.css" rel="stylesheet">
        <!-- bootstrap 3.0.2 -->
        <link rel="stylesheet" type="text/css" href="css/bootstrap-3.2.0/css/bootstrap.css" />
        <link rel="stylesheet" type="text/css" href="css/bootstrap-box.css" />
        <link rel="stylesheet" type="text/css" href="css/bootstrap-page.css" />
        <link rel="stylesheet" type="text/css" href="css/myStyle.css" rel="stylesheet" type="text/css"/>
        <link rel="stylesheet" type="text/css" href="js/datapacker/css/bootstrap-datetimepicker.min.css" />
        <link rel="stylesheet" type="text/css" href="js/resizableColumns/jquery.resizableColumns.css" rel="stylesheet" />
        <link rel="stylesheet" type="text/css" href="js/nice-validator/jquery.validator.css" />
        <link href="js/jquery-confirm/jquery-confirm.css" rel="stylesheet" type="text/css" />
        <script type="text/javascript" src="js/bootstrap.min.js"></script>
        <script type="text/javascript" src="js/jquery-confirm/jquery-confirm.js"></script>
        <script type="text/javascript" src="js/resizableColumns/jquery.resizableColumns.js"></script>
        <script type="text/javascript" src="js/jsviews.js"></script>
        <script type="text/javascript" src="js/loadTmpl.js"></script>
        <script type="text/javascript" src="js/tableGrid/jquery.tableGrid.js"></script>
        <script type="text/javascript" src="js/nice-validator/jquery.validator.min.js"></script>
        <script type="text/javascript" src="js/nice-validator/local/zh-CN.js"></script>
        <script type="text/javascript" src="js/datapacker/js/bootstrap-datetimepicker.min.js"></script>
        <script type="text/javascript" src="js/datapacker/js/locales/bootstrap-datetimepicker.zh-CN.js"></script>
        <script type="text/javascript" src="js/layer/layer.js"></script>
        <script type="text/javascript" src="js/base.js" ></script>
        <script type="text/javascript">
        	//从后台取数据，并加载模板
	        function displayContent(){
	        	var resourceName = $("#resourceName").val();
				var nextPage=$("#_click_page").val();			//不变
				if(typeof(nextPage) == 'undefined' || nextPage==""){
					nextPage=0;
				}
				var projectId=getUrlParam("projectId");
				var prjName=getUrlParam("prjName");
				if(projectId==""){
					myAlert("projectId参数不能为空");
					return;
				}
				$("#currentPrj").html(prjName);
				$("#_projectId").val(projectId);
				$("#_prjName").val(prjName);
				var jsonFilter = getfilter();
				var pageSize=$("#selectPageSize").val();
				var url=root+"model";
				$.ajax({
					url:url,
					dataType:'json',
					type:'get',
					data:{
						nextPage:nextPage,
						pageSize:pageSize,
						jsonFilter:jsonFilter
					},
					success:function(data){
						var tableData=data.object.content;
		            	var tblContentHtml = $("#tableContentTmple").render(tableData);
		            	$("#mycontent").html(tblContentHtml);
		            	//加载分页器
		            	loadTmpl.renderExternalTemplate("page", "#displayPage", data.object);

                        //活动页面加载时间日历
                        $("#startTime").datetimepicker({minView: "month",format: 'yyyy-mm-dd',language : 'zh-CN',autoclose: true}).on('changeDate',function(ev){
                            var starttime=$("#startTime").val();
                            $("#endTime").datetimepicker('setStartDate',starttime);
                            $("#startTime").datetimepicker('hide');
                        });
                        $("#endTime").datetimepicker({minView: "month",format: 'yyyy-mm-dd',language : 'zh-CN',autoclose: true}).on('changeDate',function(ev){
                            var endtime=$("#endTime").val();
                            $("#startTime").datetimepicker('setEndDate',endtime);
                            $("#endTime").datetimepicker('hide');
                        });
					}
				});
	        }

            function getfilter(){
                var projectId=getUrlParam("projectId");
                var _jsonFilter = "{";
                if(projectId && projectId.length > 0){
                    _jsonFilter += "'search_EQ_projectId':'"+projectId+"',";
                }
                if($("#resourceName").val() && $("#resourceName").val().length > 0){
                    _jsonFilter += "'search_LIKEIGNORE_name':'"+replaceSingleQuote($("#resourceName").val())+"',";
                }
                if(($("#endTime").val() && $("#endTime").val().length > 0) && $("#startTime").val().length<=0){
                    _jsonFilter += "'search_LTE_createTime':'"+$("#endTime").val()+"',";
                }
                if(($("#endTime").val() && $("#endTime").val().length > 0) && ($("#startTime").val() && $("#startTime").val().length > 0)){
                    _jsonFilter += "'search_BETWEEN_createTime':'"+$("#startTime").val()+","+$("#endTime").val()+"',";
                }
                if(_jsonFilter != "{"){
                    _jsonFilter = _jsonFilter.substring(0,_jsonFilter.length - 1);
                }
                _jsonFilter += "}";
                return _jsonFilter;
            }

			$(function(){
				$("#example2").resizableColumns({});	//列拖动
				//行高亮,选中
				$("#example2").tableGrid({
					checkAllId:"_selectAll",	//全选框的ID属性
					singleCheckboxClass:"ckSelect",
					selectRowClass:"warning",
					paging:"displayPage",
					pageAjax:function(){
						displayContent();
					}
				});
				//点击新增按钮
				$("#addSome").click(function(){
					$("#myModalLabel").html("新增实体");
					$("#modelCU")[0].reset();
					$("#_modelId").val("");	//清除因修改而记录下的modelId
					$("#modelCU").validator("cleanUp");
					$("#myModalDlg").modal("show");
				});
				//新增或修改
				$("#modelCU").validator({
					valid:function(form){
						var modelId=$("#_modelId").val();
						var projectId=$("#_projectId").val();
						var name=$("#_name").val();
						var identifiy=$("#_identify").val();
						var description=$("#_description").val();
						var model="";
						if(modelId==""){
						//新增
						
							model={"name":name,
								"identify":identifiy,
								"description":description,
								"projectId":projectId
							};
							url=root+"model";	
							$.ajax({
								url:url,
								type:"post",
								dataType: "json",
				                contentType: "application/json",
								data:JSON.stringify(model),
								success:function(data){
									if(data.code==1){
										$('#myModalDlg').modal('hide');
										displayContent();
									}else{
										myAlert(data.message || '数据加载失败，稍后重试！');
									}
								}
							});
						}else{
							//修改
							model={"id":modelId,
								"name":name,
								"identify":identifiy,
								"description":description,
								"projectId":projectId
							};
							url=root+"model";
							$.ajax({
								url:url,
								type:"PUT",
								dataType: "json",
				                contentType: "application/json",
								data:JSON.stringify(model),
								success:function(data){
									if(data.code==1){
										$('#myModalDlg').modal('hide');
										displayContent();
									}else{
										myAlert(data.message || '数据加载失败，稍后重试！');
									}
								}
							});
						}
					}
				}).on("click","#saveOrupdate",function(e){
					$(e.delegateTarget).trigger("validate");
				});
				
				//点击修改按钮
				$("#updateSome").click(function(){
					$("#modelCU").validator("cleanUp");		//清除验证信息
					var updateArray=new Array();
					$(".ckSelect").each(function(){
                    	var check=$(this).is(':checked');
                    	if(check){
							var _id =$(this).attr("ckId");
							updateArray.push(_id);
                        }
                    });
					if(updateArray.length==0){
						myAlert("请选择要编辑的项");
						return;
					}
					if(updateArray.length>1){
						myAlert("每次只能编辑一项");
						return;
					}
					$.ajax({
						url:root+"model/"+updateArray[0],
						type:"get",
						dataType: "json",
		                contentType: "application/json",
						success:function(selectData){
						
							if(selectData.code==1){
								$("#_name").val(selectData.object.name);
								$("#_identify").val(selectData.object.identify);
								$("#_description").val(selectData.object.description);
								$("#_modelId").val(selectData.object.id);
								$("#myModalLabel").html("编辑实体");
								$("#myModalDlg").modal("show");
							}else{
								myAlert(selectData.message);
							}
						}
					});
				});
				//点击属性按钮
				$("#propertiesSome").click(function(){
					var updateArray=new Array();
					var modelName="";
					$(".ckSelect").each(function(){
                    	var check=$(this).is(':checked');
                    	if(check){
							var _id =$(this).attr("ckId");
							updateArray.push(_id);
							modelName=$(this).attr("md");
                        }
                    });
					if(updateArray.length==0){
						myAlert("请选择要编辑的项");
						return;
					}
					if(updateArray.length>1){
						myAlert("每次只能编辑一项");
						return;
					}
					var projectId=$("#_projectId").val();
					var projectName=$("#_prjName").val();
					window.location.href=root+"proterties.html?modelId="+updateArray[0]+"&projectId="+projectId+"&prjName="+escape(projectName)+"&modelName="+escape(modelName);
				});
				function publishDownloadFile(id,type){
					/*$.ajax({
						url:root+"model/"+id+"/publish/"+type,
						type:"GET"
					});
					console.log(type);*/
					window.location.href=root+"model/"+id+"/publish/"+type;
				}
                function publishDownloadFiles(modelIds){
                    console.log(modelIds);
                    var modelIdsStr = { 'modelIds' : modelIds.join(',')};
                    /*$.ajax({
                        url:root+"model/publishProjectModes",
                        type:'GET',
                        data:modelIdsStr,
                        success:function(data){
                            console.log('sucess');
                            myAlert('多选模块发布完成,请到对应目录下寻找');
                        }
                    });*/
                    window.location.href=root+"model/publishProjectModes?modelIds="+modelIds.join(',');
                }
				//点击发布按钮
				$("#pubSome").click(function(){
					var updateArray=new Array();
					$(".ckSelect").each(function(){
                    	var check=$(this).is(':checked');
                    	if(check){
							var _id =$(this).attr("ckId");
							updateArray.push(_id);
                        }
                    });
					if(updateArray.length==0){
						myAlert("请选择要发布的项");
						return;
					}
					if(updateArray.length>1){
						//$.myAlert每次只能发布一项");
						//return;
                        publishDownloadFiles(updateArray);
					} else {
                        publishDownloadFile(updateArray[0],"Project");
                    }
				});
				//点击查询按钮
				$("#search").click(function(){
					displayContent();
				});
				//点击删除按钮
				$("#delSome").click(function(){
					var delStr="";
					var delArray=new Array();
					$(".ckSelect").each(function(){
                    	var check=$(this).is(':checked');
                    	if(check){
							var _id =$(this).attr("ckId");
							delStr+=parseInt(_id)+",";
							delArray.push(_id);
                        }
                    });
                    if(delStr.length==0){
						myAlert("请选择要删除的项");
						return
                    }
                    if(delArray.length>1){
                        myAlert("只能删除一项奥");
                        return
                    }
                    myConfirm("你确定要删除选中项吗？", function () {
                        var url=root+"model/"+delArray[0];
                        $.ajax({
                            url:url,
                            type:'DELETE',
                            dataType:'json',
                            success:function(data){
                                if(data.code==0){
                                    displayContent();
                                }else{
                                    myAlert(data.message || '数据加载失败，稍后重试！');
                                }
                            }
                        });
                    });
				});

                //清除时间
                $(".datetime_clear").click(function(){
                    $(this).next().val('');
                    if($(this).next().attr('id')=='startTime'){
                        var starttime=$("#startTime").val();
                        $("#endTime").datetimepicker('setStartDate',starttime);
                        $("#startTime").datetimepicker('hide');
                    }
                    if($(this).next().attr('id')=='endTime'){
                        var endtime=$("#endTime").val();
                        $("#startTime").datetimepicker('setEndDate',endtime);
                        $("#endTime").datetimepicker('hide');
                    }
                });

            });
			
        </script>
    </head>
    <body>
      <section>
          <div>
              <div class="col-xs-12">
                  <div class="box">
                      <div class="box-header">
                          <br>
                          <span class="add-on">当前工程:</span><span id="currentPrj"></span>
                      </div>
                      <div class="box-header">
                          <form id="search_form" class="form-horizontal">
                              <div class="tabSelect" id="selectHeadId">
                                  <br>
                                  <div class="form-group  col-md-3 search-form">
                                      <label class="col-md-4 control-label controls">资源名称：</label>
                                      <div class="col-md-8">
                                          <input type="text" placeholder="资源名称" class="form-control input-sm" id="resourceName"
                                                 name="content"/>
                                      </div>
                                  </div>
                                  <div class="form-group  col-md-3 search-form">
                                      <label class="col-md-4 control-label controls">建表时间：</label>
                                      <div class="col-md-8" style="padding-right:0;position:relative;">
                                          <div class="datetime_clear"></div>
                                          <input type="text" readOnly placeholder="开始时间" class="form-control input-sm" id="startTime" name="createTime"/>
                                      </div>
                                  </div>
                                  <div class="form-group  col-md-3 search-form">
                                      <label class="col-md-1 control-label controls">至</label>
                                      <div class="col-md-8" style="padding-right:0;position:relative;">
                                          <div class="datetime_clear"></div>
                                          <input type="text" readOnly placeholder="结束时间" class="form-control input-sm" id="endTime" name="createTime"/>
                                      </div>
                                  </div>
                                  <input type="button" value="查询" id="search" class="btn btn-primary searchBtn">
                              </div>
                          </form>
                      </div>
                      <div class="box-header">
                      <br>
                          <input type="button" value="新增" id="addSome" class="btn btn-success">
                          <input type="button" value="删除" id="delSome" class="btn btn-danger">
                          <input type="button" value="编辑" id="updateSome" class="btn btn-info">
                          <input type="button" value="发布" id="pubSome" class="btn">
                          <input type="button" value="属性管理" id="propertiesSome" class="btn btn-warning">
                          <a  href="projects.html" class="btn btn-info" >返回</a>
                      </div>
                      <div class="box-body table-responsive">
                          <table id="example2" class="table table-bordered table-hover">
                              <thead>
                                  <tr>
                                      <th><input type="checkbox" id="_selectAll" /></th>
                                      <th id='name'>实体中文名称</th>
                                      <th id='identify'>实体名</th>
                                      <th id='description'>实体描述</th>
                                      <th id='status'>状态</th>
                                  </tr>
                              </thead>
                              <!-- 表格内容 start -->
                              <tbody id="mycontent"></tbody>
                              <!-- 表格内容 end -->
                          </table>
                          <!-- 分页标签 start -->
                          <div class="row page_big_div" id="displayPage"></div>
                          <script id="tableContentTmple" type="text/x-jsrender">
								<tr>
									<td><input id="ck_{{:id}}" ckId="{{:id}}" md="{{:name}}" class="ckSelect" type="checkbox" /></td>
									<td>{{:name}}</td>
									<td>{{:identify}}</td>
									<td>{{:description}}</td>
									<td>{{if status==0}}正常{{else status=1}}发布{{else status=2}}草稿{{else status=3}}删除{{/if}}</td>
								</tr>
						  </script>
                  </div>
                  <input type="hidden" id="_projectId" />
                  <input type="hidden" id="_prjName" />
              </div>
          </div>
      </section>
	<!-- 新增对话框  start -->
	<form class="form-horizontal" id="modelCU" role="form">
	<div class="modal fade" id="myModalDlg" tabindex="-1" role="dialog"
		aria-labelledby="myModalLabel" aria-hidden="true">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal"
						aria-hidden="true">&times;</button>
					<h4 class="modal-title" id="myModalLabel">新增实体</h4>
				</div>
				<div class="modal-body">
						<input type="hidden" id="_modelId" />
						<div class="form-group form-group-sm">
							<label class="col-sm-2 control-label">中文名称</label>
							<div class="col-sm-4">
								<input class="form-control" type="text" id="_name" data-rule="中文名称:required;length[1~40]"
									placeholder="中文名称" />
							</div>
						</div>
						<div class="form-group form-group-sm">
							<label class="col-sm-2 control-label">实体名称</label>
							<div class="col-sm-4">
								<input class="form-control" type="text" id="_identify" data-rule="中文名称:required;length[3~40]"
									placeholder="英文，首字母大写" />
							</div>
						</div>
						<div class="form-group form-group-sm">
							<label class="col-sm-2 control-label">描述</label>
							<div class="col-sm-4">
								<textarea class="form-control" style="width: 180px;height: 160px;" id="_description" data-rule="中文名称:required;length[1~200]"></textarea>
							</div>
						</div>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default" data-dismiss="modal">关闭
					</button>
					<button type="button" id="saveOrupdate" class="btn btn-primary">确定</button>
				</div>
			</div>
		</div>
	</div>
	</form>
	<!-- 新增对话框  end -->
    </body>
</html>